package com.robusta.cityuate.objects;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.robusta.cityuate.services.DeleteSpotService;
import com.robusta.cityuate.services.FavoriteSpotService;
import com.robusta.cityuate.services.HTTPService;
import com.robusta.cityuate.services.ReportSpotService;
import com.robusta.cityuate.utils.ConnectionUtils;

public class Spot implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public double getLongt() {
		return longt;
	}

	public void setLongt(long longt) {
		this.longt = longt;
	}

	public double getLat() {
		return lat;
	}

	public void setLat(long lat) {
		this.lat = lat;
	}

	public String getImgURL() {
		return HTTPService.SERVER_URL + imgURL;
	}

	public void setImgURL(String imgURL) {
		this.imgURL = imgURL;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getLandmarks() {
		return landmarks;
	}

	public void setLandmarks(String landmarks) {
		this.landmarks = landmarks;
	}

	public static final String SPOT = "spot";
	public static final String SPOT_NAME = "name";
	public static final String SPOT_DESCRIPTION = "description";
	public static final String SPOT_LANDMARKS = "landmarks";
	public static final String SPOT_LONG = "longitude";
	public static final String SPOT_LAT = "latitude";
	public static final String SPOT_FILE = "file";
	public static final String SPOT_FILENAME = "filename";
	public static final String SPOT_ADDRESS = "address";
	public static final String SPOT_CITY = "city";
	public static final String SPOT_COUNTRY = "country";
	public static final String SPOT_IAMGE_URL = "image_url";
	public static final String SPOT_ID = "id";
	public static final String IS_FAVORITE = "current_user_favorited_spot";
	public static final String SPOT_OFFERINHGS = "offerings";
	public static final String CURRENT_USER_CREATED_SPOT = "current_user_created_spot";
	
	// private static final String TAG = SpotsListActivity.class.getName();

	String name;
	String description;
	double longt;
	double lat;
	String imgURL;
	String address;
	String landmarks;
	long id;
	boolean isCreator;
	
	public boolean isCreator() {
		return isCreator;
	}

	public void setCreator(boolean isCreator) {
		this.isCreator = isCreator;
	}

	public void setLongt(double longt) {
		this.longt = longt;
	}

	public void setLat(double lat) {
		this.lat = lat;
	}

	public void setId(long id) {
		this.id = id;
	}

	Boolean isFavorite;
	ArrayList<Offering> offerings=new ArrayList<Offering>();

	public ArrayList<Offering> getOfferings() {
		return offerings;
	}

	public void setOfferings(ArrayList<Offering> offerings) {
		this.offerings = offerings;
	}

	public static Spot createFromJSON(JSONObject jsonObject) {
		Spot spot = new Spot();
		try {
			JSONObject spotjson = jsonObject.getJSONObject(SPOT);
			spot.name = spotjson.getString(SPOT_NAME);
			spot.description = spotjson.getString(SPOT_DESCRIPTION);
			spot.landmarks = spotjson.getString(SPOT_LANDMARKS);
			spot.address = spotjson.getString(SPOT_ADDRESS);
			spot.longt = spotjson.getDouble(SPOT_LONG);
			spot.lat = spotjson.getDouble(SPOT_LAT);
			spot.imgURL = spotjson.getString(SPOT_IAMGE_URL);
			spot.id = spotjson.getLong(SPOT_ID);
			if (spotjson.has(IS_FAVORITE))
				spot.isFavorite = spotjson.getBoolean(IS_FAVORITE);
			else
				spot.isFavorite = false;
			
			if(spotjson.has(CURRENT_USER_CREATED_SPOT))
				spot.isCreator= spotjson.getBoolean(CURRENT_USER_CREATED_SPOT);
			else
				spot.isCreator=true; // if not sent assume that it's not mine
			
			if(spotjson.has(SPOT_OFFERINHGS)){
				
				JSONArray offeringsJArray=spotjson.getJSONArray(SPOT_OFFERINHGS);
				//System.out.println("loading offer list "+offeringsJArray.toString());
				for (int i = 0; i < offeringsJArray.length(); i++) {
					Offering offer=Offering.getOfferingFromJSON(offeringsJArray.getJSONObject(i),spot);
					spot.offerings.add(offer);
				}
			}
			return spot;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}

	public Boolean getIsFavorite() {
		return isFavorite;
	}

	public void setIsFavorite(Boolean isFavorite) {
		this.isFavorite = isFavorite;
	}

	public long getId() {
		return id;
	}

	public void delete(Context mContext,String tag) {
		if (!ConnectionUtils.isNetworkAvailable(mContext))// {
			// No connection
			ConnectionUtils.showNoConnectionDialog(mContext);
		// return;
		// } else { // Connection is available
		// start back-ground service to handle Create process
		Intent backgroud_regService = new Intent(mContext,
				DeleteSpotService.class);
		backgroud_regService.setAction(tag);

		backgroud_regService.putExtra(HTTPService.LOGIN_RQUIRED, true);
		backgroud_regService.putExtra(SPOT_ID, this.id);
		backgroud_regService.putExtra(DeleteSpotService.MULTIPLE_DELETE, false);
		mContext.startService(backgroud_regService);
		Log.e(tag, "Fired DeleteSpot service");

		// }
	}

	public static void deleteMultiple(Context mContext, ArrayList<Spot> spots,String tag) {
		if (spots.size() == 0)
			return;

		if (!ConnectionUtils.isNetworkAvailable(mContext)) // {
			// No connection
			ConnectionUtils.showNoConnectionDialog(mContext);
		// return;
		// } else { // Connection is available
		// start back-ground service to handle Create process
		JSONArray ids = new JSONArray();
		for (int i = 0; i < spots.size(); i++)
			ids.put(spots.get(i).getId());

		Intent backgroud_regService = new Intent(mContext,
				DeleteSpotService.class);
		backgroud_regService.setAction(tag);

		backgroud_regService.putExtra(HTTPService.LOGIN_RQUIRED, true);
		backgroud_regService.putExtra(SPOT_ID, ids.toString());
		backgroud_regService.putExtra(DeleteSpotService.MULTIPLE_DELETE, true);
		mContext.startService(backgroud_regService);
		Log.e(tag, "Fired DeleteSpot service");

		// }
	}

	@Override
	public boolean equals(Object o) {
		return ((Spot) o).id == this.id;
	}

	public void toggleFavorite(Context mContext, boolean favorite,String tag) {
		//Log.e(SpotsListActivity.FAV_TAG, "Toggle Fav");
		if (!ConnectionUtils.isNetworkAvailable(mContext)) // {
			// No connection
			ConnectionUtils.showNoConnectionDialog(mContext);
		// return;
		// } else { // Connection is available
		// start back-ground service to handle Create process
		Intent backgroud_regService = new Intent(mContext,
				FavoriteSpotService.class);
		backgroud_regService.setAction(tag);

		backgroud_regService.putExtra(HTTPService.LOGIN_RQUIRED, true);
		backgroud_regService.putExtra(SPOT_ID, this.id);
		backgroud_regService
				.putExtra(FavoriteSpotService.TO_FAVORITE, favorite);
		mContext.startService(backgroud_regService);
		Log.e(tag, "Fired FavoriteSpot service");

		// }

	}

	public void report(Context mContext,String tag) {
		if (!ConnectionUtils.isNetworkAvailable(mContext)) // {
			// No connection
			ConnectionUtils.showNoConnectionDialog(mContext);
		// return;
		// } else { // Connection is available
		// start back-ground service to handle Create process
		Intent backgroud_regService = new Intent(mContext,
				ReportSpotService.class);
		backgroud_regService.setAction(tag);

		backgroud_regService.putExtra(HTTPService.LOGIN_RQUIRED, true);
		backgroud_regService.putExtra(SPOT_ID, this.id);

		mContext.startService(backgroud_regService);
		Log.e(tag, "Fired ReportSpot service");

		// }

	}

}
